using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice_Windows.Units;
using SolanumSolstice_Windows.Mapping;

namespace SolanumSolstice_Windows.Buildings
{
    public class Spawn : Building
    {

        protected float m_spawnrate = 10; //in seconds
        //protected GameTime elapsed;
        protected double spawncountdown;
        protected List<Zombie> m_zmgr;
        protected UnitClass type;

        public UnitClass UnitType
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
            }
        }
        /* easier constructor?*/
        /*
        public Spawn (Building b, float spawnrate) {
          
        }*/

        public Spawn(ContentManager content, string name, Vector2 position, Vector2 entrance, int zombieCount, float spawnrate, List<Zombie> zmgr, UnitClass spawntype)
            : base(content, name, position, entrance, zombieCount)
        {
            

            m_spawnrate = spawnrate;
            m_zmgr = zmgr;


            buildingType = BuildingType.Spawn;
            spawncountdown = spawnrate;
            type = spawntype;

            if (type == UnitClass.Civilian)
            {
                infested = content.Load<Texture2D>("Content\\Txtr\\Sys\\survivalguide");
                texture = content.Load<Texture2D>("Content\\Txtr\\Bldg\\building_zombie_shoppingmall");
                height = texture.Height;
                width = texture.Width;
            }
            else if (type == UnitClass.Military)
            {
                infested = content.Load<Texture2D>("Content\\Txtr\\Sys\\nuke");
                texture = content.Load<Texture2D>("Content\\Txtr\\Bldg\\building_zombie_military");
                height = texture.Height;
                width = texture.Width;
            }
            else if (type == UnitClass.Science)
            {
                infested = content.Load<Texture2D>("Content\\Txtr\\Sys\\doublehelix");
                texture = content.Load<Texture2D>("Content\\Txtr\\Bldg\\building_zombie_hospital");
                height = texture.Height;
                width = texture.Width;
            }
        } 

         public Zombie spawnZombie() {
             Zombie zombie=null;
             if (type == UnitClass.Civilian) {  zombie = new ZombieCivilian(base.content, new Vector2(this.entrance.X, this.entrance.Y));   }
             else if (type == UnitClass.Military) { zombie = new ZombieScientist(base.content, new Vector2(this.entrance.X, this.entrance.Y));   }
             else if (type == UnitClass.Science)   { zombie = new ZombieSoldier(base.content, new Vector2(this.entrance.X, this.entrance.Y));   }
             return (Zombie)zombie;
         }


        public override void Update(GameTime gameTime, GamePadState gamePad, GameCursor gameCursor)
        {
            base.Update(gameTime, gamePad, gameCursor);
            TimeSpan elapsed = gameTime.ElapsedGameTime;

            spawncountdown -= elapsed.Milliseconds / 1000.0f;

            if (spawncountdown < 0 && SolanumSolstice.activeZombies.Count < 140 && !isInfested)
            {
                //spawn a new Zombie
                Zombie tmpz = spawnZombie();
                //pick a random location as goal
                //then give it a heading to the closest person
                tmpz.SetTarget(Map.getRandomWalkableSpace(tmpz.Position, tmpz.SightRadius));

                m_zmgr.Add(tmpz);

                spawncountdown = m_spawnrate;
            }
        }


    }
    
}


